package app

// Watershed 流域分块
type Watershed struct {
	AreaName               string      `json:"areaName;comment:流域名称"`
	AreaSize               float64     `json:"areaSize;comment:流域集雨面积"`
	RainfallStationNum     int         `json:"rainfallStationNum;comment:雨量站个数"`
	EvaporationStationNum  int         `json:"evaporationStationNum;comment:蒸发站个数"`
	SubWatershedNum        int         `json:"subWatershedNum;comment:单元流域个数"`
	SubWatershedArea       []float64   `json:"subWatershedArea;comment:单元流域面积"`
	RainfallStationRate    [][]float64 `json:"rainfallStationRate;comment:各单元流域对应各雨量站比例，按泰森多边形分配"`
	EvaporationStationRate [][]float64 `json:"evaporationStationRate;comment:各单元流域对应各蒸发站比例，按泰森多边形分配"`

	P  [][]float64 `json:"p;comment:单元流域逐时段降雨量，mm"`
	EM [][]float64 `json:"em;comment:单元流域逐时段降雨量，mm"`
}

func (w *Watershed) SetState(para *Params) {
	w.AreaSize = para.MArea
	w.SubWatershedNum = para.MNumSubWatershed // 单元流域个数
	w.RainfallStationNum = 1
	w.RainfallStationRate = para.MRateRainfallStation
	w.EvaporationStationRate = para.MRateEvaporationStation
	w.SubWatershedArea = para.MAreaSubWatershed
	w.EvaporationStationNum = para.MNumEvaporationStation
	w.AreaName = para.MName
}

func (w *Watershed) Calculate(para *Params) {

	timeRow := para.RainfallRecordsRows // 记录条数，即时段数

	p := make([][]float64, timeRow)
	for i := range p {
		p[i] = make([]float64, w.SubWatershedNum)
	}

	for i := 0; i < timeRow; i++ {
		for j := 0; j < w.SubWatershedNum; j++ {
			for c := 0; c < w.RainfallStationNum; c++ {
				p[i][j] = para.RainfallData[c][i] * w.RainfallStationRate[j][c]
			}
		}
	}
	// 计算时间-流域的降雨二维数据
	w.P = p

	em := make([][]float64, timeRow)
	for i := range p {
		em[i] = make([]float64, w.SubWatershedNum)
	}

	for i := 0; i < timeRow; i++ {
		for j := 0; j < w.SubWatershedNum; j++ {
			for c := 0; c < w.RainfallStationNum; c++ {
				em[i][j] = para.EMData[c][i] * w.EvaporationStationRate[j][c]
			}
		}
	}
	// 计算时间-流域的蒸发二维数据
	w.EM = em

}

// GetP 获取经过泰深多边形单元格的降雨值
func (w Watershed) GetP(i int, j int) float64 {
	return w.P[i][j]
}

// GetEM 获取经过泰深多边形单元格的蒸发值
func (w Watershed) GetEM(i int, j int) float64 {
	return w.EM[i][j]
}

// GetF 获取子流域面积
func (w Watershed) GetF(j int) float64 {
	return w.SubWatershedArea[j]
}
